package com.mkz.jbpm.activiti.impl;

import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.runtime.ProcessInstanceQuery;
import org.activiti.engine.task.Task;
import org.springframework.stereotype.Component;

import com.mkz.jbpm.activiti.ActInstanceQueryService;
import com.mkz.jbpm.activiti.base.ActServiceFactory;

/**
 * ClassName: ActInstanceQueryServiceImpl 
 * @Description: 流程实例查询
 * @author: KuangJianyu
 * @date: 2020年4月3日
 */
@Component
public class ActInstanceQueryServiceImpl extends ActServiceFactory implements ActInstanceQueryService {

    @Override
    public ProcessInstanceQuery createProcessInstanceQuery() {
        return runtimeService.createProcessInstanceQuery();
    }

    @Override
    public ProcessInstance processInstanceId(String processInstanceId) {
        return createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
    }

    @Override
    public ProcessInstance processInstanceBusinessKey(String processInstanceBusinessKey) {
        return createProcessInstanceQuery().processInstanceBusinessKey(processInstanceBusinessKey).singleResult();
    }

    @Override
    public boolean hasProcessInstanceFinished(String processInstanceId) {
        ProcessInstance pi = processInstanceId(processInstanceId);
        return pi == null;
    }

    @Override
    public ProcessInstance taskId(String taskId) {
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        String processInstId = task.getProcessInstanceId();
        return this.processInstanceId(processInstId);
    }
}
